﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class MP_MasFieldPositions : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IFieldBLL fieldBLL = new FieldBLL();
        private IFieldGroupBLL groupBLL = new FieldGroupBLL();
        private IEmployeeTypeBLL empTypeBLL = new EmployeeTypeBLL();
        private FieldGroupInfo groupInfo = new FieldGroupInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    bindEmpType();
                }
            } catch (Exception ee)
            {
            }

        }
        public void LoadDataGrid()
        {
            try{
            string EmpTypeSeq = ddlEmployeeType.SelectedValue;

            FieldInfo fInfo = new FieldInfo();
            IList<FieldInfo> Info = fieldBLL.ListFields("", EmpTypeSeq);

            //for (int i = 0; i < Info.Count; i++)
            //{
            //    IList<FieldGroupInfo> grpList = groupBLL.ListFieldGroups(Info[i].FieldGroups.Sequence, null);
            //    groupInfo.Name = grpList[0].Name + " " + grpList[0].Description;
            //    Info[i].FieldGroups = groupInfo;
            //}
            RadGrid1.DataSource = Info;

            //GridGroupByExpression g = new GridGroupByExpression(RadGrid1.Columns[9]);
            //RadGrid1.MasterTableView.GroupByExpressions.Add(g);

            //GridGroupByExpression g2 = new GridGroupByExpression(RadGrid1.Columns[11]);
            //RadGrid1.MasterTableView.GroupByExpressions.Add(g2);
            
            RadGrid1.DataBind();
            //AddExpression();
            RadGrid1.Rebind();
            }
            catch (Exception ee)
            {
            }
        }
        private void bindEmpType()
        {
            try
            {
                IList<EmployeeTypeInfo> infoList = empTypeBLL.ListEmployeeTypes();
                ddlEmployeeType.DataSource = infoList;
                ddlEmployeeType.DataBind();


            }
            catch (Exception ee)
            {
            }
        }
        private void AddExpression()
        {
            try{
            GridSortExpression expression = new GridSortExpression();
            expression.FieldName = "PositionTypes.Sequence";
            expression.SetSortOrder("Descending");
            RadGrid1.MasterTableView.SortExpressions.AddSortExpression(expression);
            }
            catch (Exception ee)
            {
            }
        }

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "DeleteAll")
                {
                    GridItem[] items = RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem);
                    if (items.Length > 0)
                    {
                        ((GridCommandItem)items[0]).FireCommandEvent("DeleteSelected", "");
                    }
                    else
                    {
                        RadGrid1.Items[0].FireCommandEvent("DeleteSelected", "");
                    }
                }
                else if (e.CommandName == "Delete")
                {
                    int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);

                    fieldBLL.Delete(seq, ssMng.EmpSeqI);
                }

                LoadDataGrid();
            }
            catch (Exception ee)
            {
            }
        }

        protected void ddlStatusActive_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDataGrid();
        }
        protected void ddlEmployeeType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDataGrid();
        }
    }
}
